<style>

</style>

<template>
    <div class="">
        <Form ref="config" :model="config" :label-width="120" label-position="right" :rules="ruleConfig">
            <FormItem label="关卡等级" prop="level">
                <Input v-model.number="config.level" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="关卡名称" prop="name">
                <Input v-model="config.name" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="每题回答时间(秒)" prop="answer_time">
                <Input v-model.number="config.answer_time" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="关卡题目数" prop="num">
                <Input v-model.number="config.num" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="首答获得分数" prop="first_score">
                <Input v-model.number="config.first_score" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="后答获得分数" prop="last_score">
                <Input v-model.number="config.last_score" placeholder="请输入"></Input>
            </FormItem>
            <!-- <FormItem label="解锁星数要求" prop="stars">
                <Input v-model.number="config.stars" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="胜利可得学分" prop="points">
                <Input v-model.number="config.points" placeholder="请输入"></Input>
            </FormItem>
            <FormItem label="全对额外奖励学分" prop="extra_points">
                <Input v-model.number="config.extra_points" placeholder="请输入"></Input>
            </FormItem> -->
            <FormItem>
                <Button type="primary" @click="handleSubmit('config')">确认</Button>
                <Button type="ghost" @click="handleReset('config')" style="margin-left: 8px">重置</Button>
            </FormItem>
        </Form>
    </div>
</template>

<script>
import http from '../../../libs/http';
export default {
    data () {
        return {
            config: {

            },
            ruleConfig: {
                level: [{type:'number',required: true, message:'关卡等级不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                name: [{required: true, message:'关卡名称不能为空', trigger: 'blur'}],
                answer_time: [{type:'number',required: true, message:'每题回答时间不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                num: [{type:'number',required: true, message:'关卡题目数不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                first_score: [{type:'number',required: true, message:'首答获得分数不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                last_score: [{type:'number',required: true, message:'后答获得分数不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                /* stars: [{type:'number',required: true, message:'解锁星数要求不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                points: [{type:'number',required: true, message:'胜利可得学分不能为空', trigger: 'blur'}, {type:'number', message:'输入格式错误',trigger:'blur'}],
                extra_points: [{type:'number',required: true, message:'全对额外奖励学分不能为空', trigger: 'blur'}, { type:'number',message:'输入格式错误',trigger:'blur'}] */
            }
        }
    },
    methods: {
        handleSubmit(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    //提交后台
                    var data = {
                        level: this.config.level,
                        name: this.config.name,
                        answer_time: this.config.answer_time,
                        num: this.config.num,
                        first_score: this.config.first_score,
                        last_score: this.config.last_score,
                        /* stars: this.config.stars,
                        points: this.config.points,
                        extra_points: this.config.extra_points */
                    }
                    var id = this.config.id;
                    var url = 'admin/game/config/save/';
                    this.apiPost(url, data).then((res) => {
                        if (res.code != 200) {
                            this.handleError(res);
                        } else {
                            this.$Message.success('添加成功');
                            this.handleReset(name);  //清空表单内容
                            this.$emit('closeAdd');
                        }
                    })
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        }
    },
    mixins: [http]
}
</script>